package com.haohan.jingyuan.sys.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import com.haohan.jingyuan.sys.dto.req.QueryDictListRequest;
import com.haohan.jingyuan.sys.dto.resp.DictListResp;
import com.haohan.jingyuan.sys.dto.resp.DictTreeResp;
import com.haohan.jingyuan.sys.service.DictService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典控制器
 *
 * @ClassName DictController
 * @Description 字典相关接口控制器
 * @Author hexijun
 * @Time 2025/01/19 14:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class SystemController {

    /**
     * 字典服务
     */
    private final DictService dictService;

    /**
     * 查询字典平铺列表
     *
     * @param request 查询字典列表请求
     * @return 字典平铺列表
     */
    @NeedLogin
    @PostMapping("/sys/dict/list")
    public GwResponse<DictListResp> queryDictList(@Valid @RequestBody GwRequest<QueryDictListRequest> request) {
        log.info("DictController.queryDictList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        DictListResp dictList = dictService.queryDictList(request.getRequestData());
        GwResponse<DictListResp> response = GwResponse.success("查询字典列表成功", dictList);

        log.info("DictController.queryDictList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询字典树形结构
     *
     * @param request 查询字典列表请求
     * @return 字典树形结构
     */
    @NeedLogin
    @PostMapping("/sys/dict/tree")
    public GwResponse<List<DictTreeResp>> queryDictTree(@Valid @RequestBody GwRequest<QueryDictListRequest> request) {
        log.info("DictController.queryDictTree BEGIN ==> {}", JSONUtil.toJsonStr(request));

        List<DictTreeResp> dictTree = dictService.queryDictTree(request.getRequestData());
        GwResponse<List<DictTreeResp>> response = GwResponse.success("查询字典树形结构成功", dictTree);

        log.info("DictController.queryDictTree END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }
}
